package com.baizhi.controller;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/user/login")
    public Map<String, Object> login(User user){
        Map<String,Object> result = new HashMap<>();
        log.info("用户名: [{}]", user.getUsername());
        log.info("密码: [{}]", user.getPassword());
        try {
            User userDB = userService.login(user);
            //用来存放payload
            Map<String, String> map = new HashMap<>();
            map.put("id",userDB.getId());
            map.put("username", userDB.getUsername());
            //通过自定义的工具类生成token
            String token = JWTUtils.getToken(map);
            result.put("state",true);
            result.put("msg","登录成功!!!");
            result.put("token",token); //成功返回token信息
        } catch (Exception e) {
            e.printStackTrace();
            result.put("state","false");
            result.put("msg",e.getMessage());
        }
        return result;
    }

    @GetMapping("/test/test")
    public String test(){
        return "你好啊,兄弟！";
    }
}
//eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEiLCJleHAiOjE2MDUxODYxMDEsInVzZXJuYW1lIjoiemhhbmdzYW4ifQ.wqwRh7iUzi6T94FMtu92qrjkkgq31vU_SIrRrgoJoEg